//
//  HomeCitySelectViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/31.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "HomeCitySelectViewController.h"
#import "CityModel.h"
#import "CityCollectionViewCell.h"
@interface HomeCitySelectViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *cityLabel;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation HomeCitySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigation:@"选择城市"];
    self.view.backgroundColor = ColorWhite;
    
    [self  configView];
    [self startNetwork];
}


- (void)startNetwork
{
    _datasource = [NSMutableArray arrayWithCapacity:0];
    @weakobj(self);
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/city/GetServeCity", GW_Domain] parameters:nil completionBlockWithSuccess:^(NSDictionary *resultDict) {
        @strongobj(self);
        NSLog(@"%@", resultDict);
        for (NSDictionary *dict in resultDict[@"data"]) {
            CityModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"CityModel"];
            [self.datasource addObject:model];
        }
        [_collectionView reloadData];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
    }];
}

- (void)configView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 65, GWIDTH, 106)];
    view.backgroundColor = ColorWhite;
    [self.view addSubview:view];
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 106 + 65, GWIDTH, 10)];
    bg.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:bg];
    
    UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 16.4, 30.8, 20)];
    locationLabel.text = @"定位";
    locationLabel.textColor = ColorFrom16(0x666666);
    locationLabel.font = PFRFONT(14);
    [view addSubview:locationLabel];
    
    _cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 18.4, 65.7, 17)];
    _cityLabel.text = CITYNAME;
    _cityLabel.textColor = ColorFrom16(0xa4a4a4);
    _cityLabel.font = PFRFONT(12);
    [view addSubview:_cityLabel];
    
    
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    _collectionView.backgroundColor = ColorWhite;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[CityCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cityLabel.mas_bottom).offset(16);
        make.left.and.right.equalTo(self.view);
        make.height.greaterThanOrEqualTo(@36);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datasource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CityModel *model = self.datasource[indexPath.row];
    CityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell configViewWithModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((GWIDTH - 60) / 3, 36);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 12, 0, 18);//分别为上、左、下、右
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    CityModel *model = self.datasource[indexPath.row];
    if(self.cityBlock)
    {
        self.cityBlock(model.city_name);
    }
    [[NSUserDefaults standardUserDefaults] setObject:model.id forKey:@"cityid"];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController.navigationBar setHidden:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
